package com.earthea.common.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class CompressionUtils {
    /**
     * GZIP压缩 + Base64编码
     */
    public static String compress(byte[] data) {
        if (ArrayUtil.isEmpty(data)) {
            return "";
        }
        try {
            byte[] compressed = ZipUtil.gzip(data);
            return Base64.encode(compressed);
        } catch (Exception e) {
            log.error("压缩失败", e);
        }
        return "";
    }

    /**
     * Base64解码 + GZIP解压缩
     */
    public static byte[] decompress(String compressed) {
        if (StringUtils.isBlank(compressed)) {
            return new byte[0];
        }
        try {
            byte[] decoded = Base64.decode(compressed);
            return ZipUtil.unGzip(decoded);
        } catch (Exception e) {
            log.error("解压缩失败", e);
        }
        return new byte[0];
    }

}
